import os
from docxtpl import DocxTemplate
import xlrd
from xlutils.copy import copy
import xlwt
import datetime
class LibreOfficxlxutils():
    #dirParam = "/Volumes/软件/FHNODE/CRM/myCRM"
    def action_cfarmc_project_print_xls(self):
        #path = self.env['ir.config_parameter'].sudo().get_param('document_path_create_project') + "2.xls"
        path ="/Volumes/软件/FHCRM/myOffice/DOC/2.xls"
        workbook = xlrd.open_workbook(path,formatting_info=True)
        """workbook.sheet_names()
        sheet2 = workbook.sheet_by_name('Sheet1')
        rows = sheet2.row_values(0)
        #获取第四行内容
        cols = sheet2.col_values(0)"""
        workbooknew = copy(workbook)
        #workbooknew.sheet_names()
        ws= workbooknew.get_sheet(0)
        alignment = xlwt.Alignment()
        # Create Alignment
        # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT,
        # HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
        alignment.horz = xlwt.Alignment.HORZ_CENTER  # 水平居中
        # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
        alignment.vert = xlwt.Alignment.VERT_CENTER  # 垂直居中
        style = xlwt.XFStyle()  # Create Style
        style.alignment = alignment  # Add Alignment to Style

        #合并单元格
        #ws.write_merge(0, 0, 0, 3, 'First Merge')
        #设置日期格式
        #style = xlwt.XFStyle()
        #style.num_format_str = 'YYYY/MM/DD h:mm:ss'
        #ws.write(5, 5, datetime.datetime.now(), style)

        ws.write(1, 3, '塘南村', style)
        #ws.write(1, 1, '第二行，第二列')
       # ws.write(1, 2, '第二行，第三列')
        ws.write(4, 0, '001', style)
        ws.write(4, 4, '001', style)

        #设置日期格式
        styleformat = xlwt.XFStyle()
        #styleformat.num_format_str = 'YYYY/MM/DD h:mm:ss'
        styleformat.num_format_str = 'YYYY/MM/DD'
        styleformat.alignment = alignment
        ws.write(4, 5, datetime.datetime.now(), styleformat)
        #ws.write(4, 5, '001', style)
        ws.write(4, 6, '001', style)

        filepath1 = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f") + ".xls"
        workbooknew.save("/Volumes/软件/FHCRM/myOffice/static/PDF/"+ filepath1)
        self.fhLibreOfficeFile(filepath1,"/Volumes/软件/FHCRM/myOffice/static/PDF/");
        return filepath1

    def fhLibreOfficeFile(self,filename,dirParam):
        #status = 1
        inputdir = dirParam
        outdir =dirParam
        cmdParam ="/Applications/LibreOffice.app/Contents/MacOS/soffice"
        cmd = cmdParam + " --invisible  --convert-to pdf:writer_pdf_Export --outdir " + outdir + "  " + inputdir  + filename
        print("cmd:", cmd)
        status = os.system(cmd)
        if (status == 0):
            print("status:", status)
        return outdir+"/" + filename

